package com.feihong.yw189.http;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.FileUtils;
import com.feihong.yw189.app.App;
import com.feihong.yw189.listener.http.RequestCallBack;
import com.feihong.yw189.model.Kaika.UpLoadPicModel;

import org.xutils.common.Callback;

import java.io.File;

import lib.image.compress.TuPianYaSuoTools;
import lib.image.compress.YaSuoListener;

public class FileUploader implements RequestCallBack<String> {

    private int request;
    private String filePath;
    private FileUploaderCallBack callBack;
    private Context context;
    private String tempFilePath = null;
    private String outputPath;

    public FileUploader(Context context) {
        this.context = context;
    }

    public void uploadFile(int request, String filePath, FileUploaderCallBack callBack) throws Exception {
        this.request = request;
        if (callBack == null) {
            throw new Exception("回调不能为NULL");
        }
        if (TextUtils.isEmpty(filePath) || !(new File(filePath).exists())) {
            callBack.error(request, filePath, "文件路径不正确或者文件不存在！");
            return;
        }
        this.filePath = filePath;
        this.callBack = callBack;
        final File file = new File(filePath);
        outputPath = file.getParent() + "/" + System.currentTimeMillis() + ".jpg";
        try {
            /*
            Luban.compress(context,new File(filePath)).
                    putGear(Luban.CUSTOM_GEAR)
                    .setMaxHeight(2000)
                    .setMaxWidth(2000)
                    .launch(this);
                    */
            /**压缩优化*/
            TuPianYaSuoTools tuPianYaSuoTools = new TuPianYaSuoTools();
            tuPianYaSuoTools.YaSuo(filePath, outputPath, new YaSuoListener() {
                @Override
                public void success(String outputfile) {

                    File file = new File(outputfile);
                    if (file.exists()) {
                        tempFilePath = outputfile;
                        App.getApp().getHttpTools().uploadFile(file, FileUploader.this);
                    } else {
                        FileUploader.this.callBack.error(FileUploader.this.request, FileUploader.this.filePath, "压缩图片出错，图片不存在!请重试");
                    }
                }

                @Override
                public void error(String error) {
                    FileUploader.this.callBack.error(FileUploader.this.request, FileUploader.this.filePath, "压缩图片失败!请重试");
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
            callBack.error(request, filePath, "无法压缩图片！");
            return;
        }
    }

    @Override
    public void onSuccess(String result) {
        Log.i("图片上传", "onSuccess: " + result);
        try {
            UpLoadPicModel upLoadPicModel = JSON.parseObject(result, UpLoadPicModel.class);
            if (upLoadPicModel == null) {
                callBack.error(request, filePath, "图片上传失败，服务端返回的数据不正确！");
                return;
            }
            if (upLoadPicModel.getStatus() != 1) {
                callBack.error(request, filePath, "图片上传失败，错误信息:" + upLoadPicModel.getInfo() + " 错误代码:" + upLoadPicModel.getStatus());
                return;
            }
            callBack.success(request, filePath, upLoadPicModel, result);

        } catch (Exception ex) {
            callBack.error(request, filePath, "图片上传失败，服务端返回的数据不正确！");
            return;
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        callBack.error(request, filePath, "图片上传失败!网络错误，请重试！");
    }

    @Override
    public void onCancelled(Callback.CancelledException cex) {

    }

    @Override
    public void onFinished() {

        try {
            if (tempFilePath != null) {
                FileUtils.deleteFile(tempFilePath);
            }
        } catch (Exception ex) {
        }
    }
}



